<script type="text/javascript" language="javascript" charset="utf-8">
$(".menu-wrap li").removeClass("active-link");
$(".menu-wrap #potential_student").addClass("active-link");
</script>
<div class="common-tit">
  <h2>添加意向学员</h2>
  <span><%= link_to "招生管理", admin_potential_students_path %> &gt; 添加意向学员</span>
</div>
<div class="cur-box">
  <h3>
    <span>基本信息</span>
  </h3>
  <div class="cur-basic clearfix">
    <div class="ads-info clearfix">
      <div  id="upload_avatar_container" class="camera">
        <div class="ca">
            <%= image_tag("/avatar.png", size: "150x150", alt: "头像" ,id: "potential_student_avatar_img")%>
        </div>
        <div  id="upload_avatar" class="upl">本地上传</div>
        <input type="hidden" id="potential_student_avatar" value="">
      </div>
      <ul class="bs-box">
        <li>
          <input type="text" class="in-l required" placeholder="姓名">
          <input type="text" class="in-r" placeholder="别名 / 英文名">
        </li>
        <li>
          <div class="list-box b-sex" choice="mandatory">
            <div class="downbtn">
              <span>请选择性别</span>
              <em></em>
            </div>
            <ul class="dropdown-menu">
              <li class="cho-tit">请选择性别</li>
              <% Company::GENDER.each do |key,value| %>
                <li data_id="<%= key %>"><%= value %></li>
              <%end%>
            </ul>
          </div>
          <b>请选择</b>
          <div class="list-box group spe clearfix" choice="mandatory">
            <div class="downbtn">
              <span>学员分组</span>
              <em></em>
            </div>
            <ul class="dropdown-menu">
              <li class="cho-tit">学员分组</li>
              <% @student_group.each do|sg| %>
                <li data_id="<%= sg.id %>"><%= sg.group_name %></li>
              <% end %>
            </ul>
          </div>
        </li>
        <li>
          <input id="stuTel" type="text" class="in-l required" placeholder="手机号">
          <input type="text" class="in-r" placeholder="微信号">
        </li>
        <li>
          <div class="date-ibox dr">
            <input type="text" class="in-l date-input" readOnly="true" placeholder="出生年月">
          </div>
          <div class="list-box group" choice="mandatory">
            <div class="downbtn">
              <span>信息来源</span>
              <em></em>
            </div>
            <ul class="dropdown-menu">
              <li class="cho-tit">信息来源</li>
              <% Company::SOURCE_FROM.each do |key,value| %>
                <li data_id="<%= key %>"><%= value %></li>
              <% end -%>
            </ul>
          </div>
        </li>
        <li>
          <input type="text" class="in-l" placeholder="紧急联系人姓名">
          <input type="text" class="in-r" placeholder="紧急联系人电话">
        </li>
        <li>
          <input type="text" class="in-a" placeholder="联系地址">
        </li>
        <li>
          <input type="text" class="in-a" placeholder="学习需求">
        </li>
        <li>
          <input type="text" class="in-a" placeholder="经济状况">
        </li>
        <li>
          <input type="text" class="in-a" placeholder="学习意愿">
        </li>
      </ul>	
    </div>
  </div>
  <h3>
    <span>相关信息</span>
  </h3>
  <div class="ad-stu clearfix">
    <div class="multiselect-box">
      <div class="mul-downbtn">
        <em></em>
        <input type="text" placeholder="搜索">
      </div>
      <div class="multiselect-down">
        <ul>
          <% @teachers.each do |teacher| %>
          <li>
            <span></span>
            <strong><%= teacher.name %></strong>
          </li>
          <% end %>
        </ul>
        <div class="mul-btn clearfix">
          <div class="mul-confirm">确认</div>
          <div class="mul-cancel">取消</div>
        </div>
      </div>
    </div>
    <div class="list-box spe clearfix" choice="mandatory">
      <div class="downbtn">
        <span>意向分店</span>
        <em></em>
      </div>
      <ul class="dropdown-menu">
        <li class="cho-tit">意向分店</li>
        <% @studios.each do |studio| %>
          <li data_id="<%= studio.id %>"><%= studio.name %></li>
        <% end %>
      </ul>
    </div>
  </div>
  <%= tag(:input, :type => 'hidden', :name => 'utf-8',:value => "✓") %>
  <%= tag(:input, :type => "hidden", :name =>  request_forgery_protection_token.to_s, :value =>   form_authenticity_token) %> 
  <div class="intention-btn">保存</div>
</div>
<script src="/assets/js/common.js"></script>
<script>
$(function(){
  $('.date-list1').date_input();
  $(".date-list2").date_input();
  $(".date-input").date_input();
});
$('.date-list2').on("click", function() {
  updateT1();
  $('.nav span').on("click", function() {
    if ($(this).parent().parent().parent().prev().attr("class") == "date-list2") {
      updateT1();
    };
  });
});
$('.date-list1').on("click", function() {
  updateT2();
  $('.nav span').on("click", function() {
    if ($(this).parent().parent().parent().prev().attr("class") == "date-list1") {
      updateT2();
    };
  });
});
$(".intention-btn").on("click", function() {
  if(adstuSet()) {
    console.log("okokokokok");
    var postJson = {
      "potential_student" : [{
        "name" : $(".bs-box input").eq(0).val(),
        "alias_name" : $(".bs-box input").eq(1).val(),
        "sex" : $(".b-sex").find("span").text(),
        "student_group" : $(".bs-box .list-box").eq(1).find("span").attr("data_id"),
        "mobile" : $(".bs-box input").eq(2).val(),
        "weixin" : $(".bs-box input").eq(3).val(),
        "birthday" : $(".bs-box input").eq(4).val(),
        "source_from" : $(".bs-box .list-box").eq(2).find("span").text(),
        "emergency_contact" : $(".bs-box input").eq(5).val(),
        "emergency_mobile" : $(".bs-box input").eq(6).val(),
        "address" : $(".bs-box input").eq(7).val(),
        "learning_requirement" : $(".bs-box input").eq(8).val(),
        "state_of_business" : $(".bs-box input").eq(9).val(),
        "learning_intention" : $(".bs-box input").eq(10).val(),
        "willing_studio" : $(".ad-stu .list-box").find("span").attr("data_id"),
        "avatar" : $("#potential_student_avatar").val(),
        "willing_teacher" : []
      }],
      'utf-8': $("input[name=utf-8]").val(),
      'authenticity_token': $("input[name=authenticity_token]").val()
    };
    var arr = [];
    for (var i = 0; i < $(".ad-stu .multiselect-down").find(".mul-select").length; i++) {
      arr.push($($(".ad-stu .multiselect-down").find(".mul-select")[i]).children("strong").text());
    };
    postJson.potential_student[0].willing_teacher = arr;
  }else{
    return;
  }
  console.log("提交内容为:"+JSON.stringify(postJson));
  console.log(postJson);

  // 意向学员唯一性验证
  student_unique_check(function(flag){
    if(flag){
      $.ajax({
          data: postJson,
          type:'post', url:"/admin/potential_students",
          success: function(data) {
            if (data.error) {
              // console.log("wrong");
            } 
          }
      });
    }
  });

})
// 验证是否必填
function adstuSet() {
  var writeIn = true;
  for (var i = 0; i < $(".bs-box .required").length; i++) {
    if ($($(".required")[i]).val() == "") {
      $($(".required")[i]).css({
        "border" : "1px solid #ff5a60" 
      });
      writeIn = false;
    } else {
      $($(".required")[i]).css({
        "border" : "1px solid #9ea0b7" 
      });
    }
  }
  if ($("#stuTel").val() != "") {

  };
  if ($(".b-sex").attr("choice") == "mandatory") {
    console.log($(".b-sex").next("b"));
    $(".b-sex").next("b").css({
      "display" : "inline"
    });
    writeIn = false;
  }
  if (true) {};
  // 添加 ajax 如果
  return writeIn;
}

function student_unique_check(callback){

    function verification(num) {
      var r = {
        user: /^1[34578]\d{9}$/
      };
      if (!r.user.test(num)) {
        return false;
      } else {
        return true;
      }
    }
    if (verification($("#stuTel").val())) {
      $.ajax({
        type:'get', url:"/admin/potential_students/check_student?mobile="+ $(".bs-box input").eq(2).val() ,
        success: function(data) {
          if (data.present) {
            alert("您的手机号已经注册过");
            $("#stuTel").css({
              "border" : "1px solid #ff5a60"
            });
            callback(false);
          }
          else {
            //如果合格则
            $("#stuTel").css({
              "border" : "1px solid #9ea0b7"
            });
            callback(true);
          }
        }
      });
    } else {
      $("#stuTel").css({
        "border" : "1px solid #ff5a60"
      });
      writeIn = false;
    }
}



</script>
